package com.zjw.algorithm.ready1;

import com.zjw.algorithm.utils.ListNode;
import com.zjw.algorithm.utils.TreeNode;

import java.util.List;
import java.util.Stack;

public class _反转链表 {

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }

        Stack<ListNode> stack = new Stack<>();

        ListNode p = head;

        while (p != null) {
            stack.push(p);
            p = p.next;
        }

        p = stack.pop();
        p.next = null;
        ListNode root = p;
        while (!stack.isEmpty()) {
            ListNode node = stack.pop();
            p.next = node;
            node.next = null;
            p = node;
        }
        return root;
    }


    public ListNode reverseList2(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode p1 = head;
        ListNode p2 = p1.next;


        while (p2 != null) {
            //只有头部断开联系
            if (p1 == head) {
                p1.next = null;
            }
            ListNode node = p2.next;
            p2.next = p1;
            p1 = p2;
            p2 = node;
        }

        return p1;
    }
}
